#include <bits/stdc++.h>

class DSU {
    mutable std::vector<int> dsu_;
    std::vector<int> siz_;
public:
    DSU(std::size_t size) : dsu_(size), siz_(size, 1) {
        std::iota(dsu_.begin(), dsu_.end(), 0);
    }
    int root(int x) const {
        int r = x;
        while (dsu_[r] != r)
            r = dsu_[r];
        while (dsu_[x] != r) {
            int y = dsu_[x];
            dsu_[x] = r;
            x = y;
        }
        return r;
    }
    void unite(int x, int y) {
        using std::swap;
        x = root(x);
        y = root(y);
        if (siz_[x] < siz_[y])
            swap(x, y);
        dsu_[y] = x;
        siz_[x] += siz_[y];
    }
    
};


int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    int n, m, k;
    std::cin >> n >> m >> k;
    DSU dsu(n);
    std::vector<std::tuple<int, int, int> > ed;
    for (int i = 0; i < 2 * m; ++i) {
        int u, v, p;
        std::cin >> u >> v >> p;
        --u, --v;
        if (u < v)
            ed.emplace_back(p, u, v);
    }
    for (int i = 0; i + 1 < k; ++i) {
        ed.emplace_back(0, i, i + 1);
    }
    std::sort(ed.begin(), ed.end());
    int64_t cost = 0;
    for (auto x : ed) {
        int w, u, v;
        std::tie(w, u, v) = x;
        if (dsu.root(u) != dsu.root(v)) {
            dsu.unite(u, v);
            cost += w;
        }
    }
    std::cout << cost << '\n';
}
